import Vue from 'vue';

Vue.filter('parseTime', (time) => {
  if (arguments.length === 0 || !time) {
    return null;
  }
  if (typeof time === 'string') {
    return time;
  }
  const format = '{y}-{m}-{d} {h}:{i}:{s}';
  const formatObj = {
    y: time.getFullYear(),
    m: time.getMonth() + 1,
    d: time.getDate(),
    h: time.getHours(),
    i: time.getMinutes(),
    s: time.getSeconds()
  }
  const timeStr = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key];
    return value.toString().padStart(2, '0');
  });
  return timeStr;
});